package screen;

import information.Admin;

import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

import javax.swing.JFrame;
import javax.swing.JOptionPane;

import panel.Window;

@SuppressWarnings("serial")
public class Main extends JFrame implements MouseListener, MouseMotionListener {

	/*Panel*/
	Window window;
	
	/*mouse*/
	int px, py, rx, ry, mx, my, dx, dy;
	boolean isDrag;
	
	/*clean*/
	boolean canClean = true;			//value : prevent from fist flickering
	
	public Main() {

		/* Panel */
		window = new Window(this);
		setContentPane(window);

		/* Frame */
		setSize(800, 500);
		setUndecorated(true);
		setLocationRelativeTo(null);
		setVisible(true);
		
		/* Listener */
		addMouseListener(this);
		addMouseMotionListener(this);
	}

	@Override
	public void mouseClicked(MouseEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mousePressed(MouseEvent e) {
		// TODO Auto-generated method stub
		px = e.getX();
		py = e.getY();

		/* exit & iconified & drag */
		if (px >= 756 && px <= 772 && py >= 3 && py <= 19)
			setState(ICONIFIED);
		else if (px >= 777 && px <= 793 && py >= 3 && py <= 19)
			System.exit(1);
		else if (py <= 23)
			isDrag = true;
		/*login about person*/
		else if(px>=34 && px<=85 && py>=151 && py<=159){
			if(px>=34 && px<=41 && py>=151 && py<=158)
				window.getLogin().addPerson();
			else if(px>=49 && px<=59 && py>=151 && py<=156)
				window.getLogin().editPerson();
			else if(px>=64 && px<=71 && py>=151 && py<=158)
				window.getLogin().deletePerson();
			else if(px>=80 && px>=85 && py>=150 && py<=159 )
				window.getLogin().searchPerson();
		}
		/*login*/
		else if(px>=153 && px<=189 && py>=144 && py<=162){
			if(window.isStateLogin()){	
				window.setStateLogin(false);
				try {
					Thread.sleep(500);					//wait 0.5s
				} catch (InterruptedException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
			else{
				window.getLogin().getConnect();
				try {
					Thread.sleep(500);					//wait 0.5s
				} catch (InterruptedException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
		}
		/* building management */
		else if (px >= 151 && px <= 191 && py >= 465 && py <= 475) {
			if (window.isStateLogin() && (window.getPerson() instanceof Admin)) {
				if(px>=151 && px<=161 && py>=465 && py<=475)
					window.getMenu().addBuilding();
				else if (px >= 166 && px <= 176 && py >= 468 && py <= 472) {
					if (window.getBuilding() == null) {
						JOptionPane.showMessageDialog(null,
								"You didn't select building", "Error",
								JOptionPane.ERROR_MESSAGE);
					} else {
						window.getMenu().editBuilding();
					}
				}
				else if(px>=182 && px<=191 && py>=465 && py<=474){
					if (window.getBuilding() == null) {
						JOptionPane.showMessageDialog(null,
								"You didn't select building", "Error",
								JOptionPane.ERROR_MESSAGE);
					} else {
						window.getMenu().deleteBuilding();
					}
				}
			} else {
				JOptionPane.showMessageDialog(null, "You don't have authority",
						"Error", JOptionPane.ERROR_MESSAGE);
			}
			window.repaint();
		}
		/* building listing */
		else if (px >= 103 && px <= 118 && py >= 214 && py <= 471) {
			if (py >= 214 && py <= 221) {
				//work anything...
			} else if (py >= 464 && py <= 471) {
				//work anything...
			}
		}
	}

	@Override
	public void mouseReleased(MouseEvent e) {
		// TODO Auto-generated method stub
		if (isDrag)
			isDrag = false;
	}

	@Override
	public void mouseEntered(MouseEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseExited(MouseEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseDragged(MouseEvent e) {
		// TODO Auto-generated method stub
		if (isDrag) {
			dx = e.getXOnScreen();
			dy = e.getYOnScreen();

			setLocation(dx - px, dy - py);
		}
	}

	@Override
	public void mouseMoved(MouseEvent e) {
		// TODO Auto-generated method stub
		mx = e.getX();
		my = e.getY();

		/* exit & iconified */
		if (mx >= 756 && mx <= 793 && my >= 3 && my <= 19) {
			if (mx >= 756 && mx <= 772) {
				window.setStateButtonIconified(1);
				window.setStateButtonExit(0);
			} else if (mx >= 777 && mx <= 793) {
				window.setStateButtonIconified(0);
				window.setStateButtonExit(1);
			} 
			
			window.repaint();
			canClean = true;
		} 
		/*login*/
		else if(mx>=153 && mx<=189 && my>=144 && my<=162){
			window.setStateButtonLogin(1);
			window.setStateButtonLogout(1);
			
			window.repaint();
			canClean = true;
		}
		/*menu*/
		else if(mx>=35 && mx<=185 && my>=229 && my<=453){
			if(my>=229 && my<=273)
				window.setStateButtonMenu(0, 1);
			 else if (my >= 289 && my <= 333) 
				window.setStateButtonMenu(1, 1);
			 else if (my >= 349 && my <= 393) 
				window.setStateButtonMenu(2, 1);
			 else if (my >= 409 && my <= 453) 
				window.setStateButtonMenu(3, 1);
			
			window.repaint();
			canClean = true;
		}
		/*clean*/
		else if(canClean){
			
			window.setStateButtonIconified(0);
			window.setStateButtonExit(0);
			window.setStateButtonLogin(0);
			window.setStateButtonLogout(0);
			window.setStateButtonMenu(0, 0);
			window.repaint();
			canClean = false;
		}
	}
}

